﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using socialnexus.classes.models;
using socialnexus.classes.pages;
using System.IO;

namespace socialnexus.admin.Users
{
    public partial class Edit : ProtectedPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SiteUser su = SiteUser.GetById(Request.QueryString["UserId"].ToString());
            if (su == null)
            {
                Response.Redirect("~/admin/Users/");
                Response.Close();
            }
            else
            {
                if (!IsPostBack)
                {
                    hfUserId.Value = Request.QueryString["UserId"].ToString();
                    txtFirstName.Text = su.FirstName;
                    txtLastName.Text = su.LastName;
                    txtPictureURL.Text = su.PictureURL;
                    txtUsername.Text = su.Username;
                }
            }

            if (rblPictureType.SelectedItem == null)
                rblPictureType.SelectedValue = "File";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Page.Validate("UserData");
            if (Page.IsValid)
            {
                SiteUser su = SiteUser.GetById(hfUserId.Value);
                su.FirstName = txtFirstName.Text;
                su.LastName = txtLastName.Text;
                su.PictureURL = txtPictureURL.Text;
                if (fuPicture.HasFile) {
                    string path = Path.Combine(MapPath(Request.ApplicationPath), @"static\images\users\");
                    string extension = fuPicture.FileName.Split('.').Last<string>();
                    string filename = Guid.NewGuid().ToString() + "." + extension;

                    fuPicture.SaveAs(Path.Combine(path, filename));

                    if (!String.IsNullOrEmpty(su.PictureURL) && !su.PictureURL.Contains("://"))
                    {
                        string oldPicture = su.PictureURL.Split('/').Last<string>();
                        try {
                            File.Delete(Path.Combine(path, oldPicture));
                        } catch { }
                    }

                    su.PictureURL = "/static/images/users/" + filename;
                }

                su.Update();
                Response.Redirect("~/admin/Users/Show.aspx?UserId=" + su.SiteUserId);
                Response.Close();
            }
        }

        protected void rblPictureType_SelectedIndexChanged(object sender, EventArgs e)
        {
            tab1Content.Visible = rblPictureType.SelectedValue == "File";
            tab2Content.Visible = rblPictureType.SelectedValue == "URL";
        }


        protected void btnPasswordChange_Click(object sender, EventArgs e)
        {
            Page.Validate("Password");
            if (Page.IsValid)
            {
                AdminUser au = AdminUser.GetById(hfUserId.Value);
                if (!String.IsNullOrEmpty(txtPassword.Text) &&
                    !String.IsNullOrEmpty(txtPassword.Text) &&
                    txtPassword.Text == txtPasswordConfirm.Text)
                {
                    au.Password = txtPassword.Text;
                    au.Update();

                    Response.Redirect("~/admin/Administrators/Show.aspx?UserId=" + au.AdminUserId);
                    Response.Close();
                }
                else
                {
                    lblPasswordError.Visible = true;
                    lblPasswordError.Text = "Passwords do not match.";
                }
            }
        }

        protected void ValidatePassword(object sender, ServerValidateEventArgs e)
        {
            e.IsValid = (txtPassword.Text == txtPasswordConfirm.Text);
        }
    }
}
